package net.csdn.business.notify.handler.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.notify.common.enums.ProcessTypeEnum;
import net.csdn.business.notify.common.model.vo.NotifyResponseVO;
import net.csdn.business.notify.common.pipeline.ProcessContext;
import net.csdn.business.notify.common.pipeline.ProcessController;
import net.csdn.business.notify.common.pipeline.domain.PrivateTaskInfo;
import net.csdn.business.notify.common.pipeline.domain.ProcessModel;
import net.csdn.business.notify.common.pipeline.domain.TaskInfo;
import net.csdn.business.notify.handler.pending.DirectTask;
import net.csdn.business.notify.handler.pending.Task;
import net.csdn.business.notify.handler.pending.TaskPendingHolder;
import net.csdn.business.notify.handler.service.IMqConsumeService;
import net.csdn.business.notify.handler.utils.GroupIdMappingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * @ClassName MqConsumeServiceImpl
 * @Description mq消费service
 * @Author zhangyl
 * @Date 2023/11/30 14:37
 */
@Slf4j
@Service
public class MqConsumeServiceImpl implements IMqConsumeService {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private TaskPendingHolder taskPendingHolder;


    @Autowired
    @Qualifier("handlerProcessController")
    private ProcessController processController;


    @Override
    public void consumeRemindMsg(TaskInfo taskInfo) {
        String topicGroupId = GroupIdMappingUtils.getGroupIdByTaskInfo(taskInfo);
        Task task = context.getBean(Task.class).setTaskInfo(taskInfo);
        taskPendingHolder.route(topicGroupId).execute(task);
    }

    @Override
    public void consumeDirectMsg(TaskInfo taskInfo) {
        //执行直达消息处理的责任链
        String topicGroupId = GroupIdMappingUtils.getGroupIdByTaskInfo(taskInfo);
        DirectTask task = context.getBean(DirectTask.class).setTaskInfo(taskInfo);
        taskPendingHolder.route(topicGroupId).execute(task);
    }

    @Override
    public void consumePrivateMsg(PrivateTaskInfo taskInfo) {
        //执行私信处理的责任链
        ProcessContext<ProcessModel> context = ProcessContext.builder()
                .processModel(taskInfo)
                .processType(ProcessTypeEnum.HANDLER_PRIVATE_NOTIFY.getCode())
                .needBreak(false).response(NotifyResponseVO.success())
                .build();
        processController.process(context);
    }
}
